package spring.cloud.tasks.tasks_assign_manager.schedule;

import lombok.Getter;
import lombok.Setter;
import org.apache.curator.framework.CuratorFramework;
import spring.cloud.tasks.common.node.TaskConfigurationNode;
import spring.cloud.tasks.common.node.TaskNodePath;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Setter
@Getter
public class TaskSignatureManager {
    private final String nameSpace;
    protected CuratorFramework curatorFramework;

    public TaskSignatureManager(String nameSpace, CuratorFramework curatorFramework) {
        this.nameSpace = nameSpace;
        this.curatorFramework = curatorFramework;
    }

    public final ConcurrentHashMap<String, TaskSignature> taskIdToSignatureMap = new ConcurrentHashMap<String, TaskSignature>();

    public void init(List<String> lastEnableTaskIdList) throws Exception {
        for (String lastEnableTaskId : lastEnableTaskIdList) {
            String taskTypeNodePath = TaskNodePath.getTaskConfigurationNodePath(lastEnableTaskId, TaskConfigurationNode.task_type);
            String taskSignatureNodePath = TaskNodePath.getTaskConfigSignatureNodePath(lastEnableTaskId);
            String taskType = String.valueOf(curatorFramework.getData().forPath(taskTypeNodePath));
            String signature = String.valueOf(curatorFramework.getData().forPath(taskSignatureNodePath));
            TaskSignature taskSignature = new TaskSignature();
            taskSignature.setTaskId(lastEnableTaskId);
            taskSignature.setTaskType(taskType);
            taskSignature.setSignature(signature);
            taskIdToSignatureMap.put(lastEnableTaskId, taskSignature);
        }

    }
}
